#!/usr/bin/env Rscript

library(tidyverse)
library(lubridate)
library(padr)

complete <- read_csv2("../app/data/completo.csv", na = "NA",
                      col_types = cols(
                          pais = col_character(),
                          regiao = col_character(),
                          uf = col_character(),
                          mesorregiao = col_character(),
                          microrregiao = col_character(),
                          municipio = col_character(),
                          st_acidente_feriado = col_character(),
                          ds_agente_causador = col_character(),
                          ano_cat = col_integer(),
                          ds_cnae_classe_cat = col_character(),
                          dt_acidente = col_date(),
                          st_dia_semana_acidente = col_character(),
                          ds_emitente_cat = col_character(),
                          hora_acidente = col_time(),
                          idade_cat = col_integer(),
                          cd_indica_obito = col_character(),
                          ds_natureza_lesao = col_character(),
                          ds_cbo = col_character(),
                          ds_parte_corpo_atingida = col_character(),
                          cd_tipo_sexo_empregado_cat = col_character(),
                          ds_tipo_acidente = col_character(),
                          ds_tipo_local_acidente = col_character()
                      ))

#Summarization of the number of accidents occurred by day, each year
#2012
ac_mun_2012 <- complete %>%
    group_by(microrregiao, municipio, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(microrregiao, municipio, n)

ac_micro_2012  <- complete %>%
    group_by(mesorregiao, microrregiao, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(mesorregiao, microrregiao, n)

ac_meso_2012 <- complete %>%
    group_by(uf, mesorregiao, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(uf, mesorregiao, n)

ac_uf_2012 <- complete %>%
    group_by(regiao, uf, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(regiao, uf, n)

ac_regiao_2012 <- complete %>%
    group_by(pais, regiao, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, regiao, n)

ac_pais_2012 <- complete %>%
    group_by(pais, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2012-01-01"),
        end_val = ymd("2012-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, n)

#2013
ac_mun_2013 <- complete %>%
    group_by(microrregiao, municipio, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(microrregiao, municipio, n)

ac_micro_2013  <- complete %>%
    group_by(mesorregiao, microrregiao, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(mesorregiao, microrregiao, n)

ac_meso_2013 <- complete %>%
    group_by(uf, mesorregiao, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(uf, mesorregiao, n)

ac_uf_2013 <- complete %>%
    group_by(regiao, uf, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(regiao, uf, n)

ac_regiao_2013 <- complete %>%
    group_by(pais, regiao, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, regiao, n)

ac_pais_2013 <- complete %>%
    group_by(pais, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2013-01-01"),
        end_val = ymd("2013-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, n)

#2014
ac_mun_2014 <- complete %>%
    group_by(microrregiao, municipio, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(microrregiao, municipio, n)

ac_micro_2014  <- complete %>%
    group_by(mesorregiao, microrregiao, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(mesorregiao, microrregiao, n)

ac_meso_2014 <- complete %>%
    group_by(uf, mesorregiao, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(uf, mesorregiao, n)

ac_uf_2014 <- complete %>%
    group_by(regiao, uf, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(regiao, uf, n)

ac_regiao_2014 <- complete %>%
    group_by(pais, regiao, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, regiao, n)

ac_pais_2014 <- complete %>%
    group_by(pais, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2014-01-01"),
        end_val = ymd("2014-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, n)

#2015
ac_mun_2015 <- complete %>%
    group_by(microrregiao, municipio, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(microrregiao, municipio, n)

ac_micro_2015  <- complete %>%
    group_by(mesorregiao, microrregiao, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(mesorregiao, microrregiao, n)

ac_meso_2015 <- complete %>%
    group_by(uf, mesorregiao, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(uf, mesorregiao, n)

ac_uf_2015 <- complete %>%
    group_by(regiao, uf, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(regiao, uf, n)

ac_regiao_2015 <- complete %>%
    group_by(pais, regiao, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, regiao, n)

ac_pais_2015 <- complete %>%
    group_by(pais, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2015-01-01"),
        end_val = ymd("2015-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, n)

#2016
ac_mun_2016 <- complete %>%
    group_by(microrregiao, municipio, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(microrregiao, municipio, n)

ac_micro_2016  <- complete %>%
    group_by(mesorregiao, microrregiao, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(mesorregiao, microrregiao, n)

ac_meso_2016 <- complete %>%
    group_by(uf, mesorregiao, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(uf, mesorregiao, n)

ac_uf_2016 <- complete %>%
    group_by(regiao, uf, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(regiao, uf, n)

ac_regiao_2016 <- complete %>%
    group_by(pais, regiao, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, regiao, n)

ac_pais_2016 <- complete %>%
    group_by(pais, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    count(dt_acidente) %>%
    pad(start_val = ymd("2016-01-01"),
        end_val = ymd("2016-12-31"),
        break_above = 2) %>%
    fill_by_value(n, 0) %>% ungroup() %>%
    select(pais, n)

# Write all csvs
# 2012
write_delim(ac_mun_2012, "../app/data/barchart/ac_mun_2012.csv", delim = ";")
write_delim(ac_micro_2012, "../app/data/barchart/ac_micro_2012.csv", delim = ";")
write_delim(ac_meso_2012, "../app/data/barchart/ac_meso_2012.csv", delim = ";")
write_delim(ac_uf_2012, "../app/data/barchart/ac_uf_2012.csv", delim = ";")
write_delim(ac_regiao_2012, "../app/data/barchart/ac_regiao_2012.csv", delim = ";")
write_delim(ac_pais_2012, "../app/data/barchart/ac_pais_2012.csv", delim = ";")
# 2013
write_delim(ac_mun_2013, "../app/data/barchart/ac_mun_2013.csv", delim = ";")
write_delim(ac_micro_2013, "../app/data/barchart/ac_micro_2013.csv", delim = ";")
write_delim(ac_meso_2013, "../app/data/barchart/ac_meso_2013.csv", delim = ";")
write_delim(ac_uf_2013, "../app/data/barchart/ac_uf_2013.csv", delim = ";")
write_delim(ac_regiao_2013, "../app/data/barchart/ac_regiao_2013.csv", delim = ";")
write_delim(ac_pais_2013, "../app/data/barchart/ac_pais_2013.csv", delim = ";")
# 2014
write_delim(ac_mun_2014, "../app/data/barchart/ac_mun_2014.csv", delim = ";")
write_delim(ac_micro_2014, "../app/data/barchart/ac_micro_2014.csv", delim = ";")
write_delim(ac_meso_2014, "../app/data/barchart/ac_meso_2014.csv", delim = ";")
write_delim(ac_uf_2014, "../app/data/barchart/ac_uf_2014.csv", delim = ";")
write_delim(ac_regiao_2014, "../app/data/barchart/ac_regiao_2014.csv", delim = ";")
write_delim(ac_pais_2014, "../app/data/barchart/ac_pais_2014.csv", delim = ";")
# 2015
write_delim(ac_mun_2015, "../app/data/barchart/ac_mun_2015.csv", delim = ";")
write_delim(ac_micro_2015, "../app/data/barchart/ac_micro_2015.csv", delim = ";")
write_delim(ac_meso_2015, "../app/data/barchart/ac_meso_2015.csv", delim = ";")
write_delim(ac_uf_2015, "../app/data/barchart/ac_uf_2015.csv", delim = ";")
write_delim(ac_regiao_2015, "../app/data/barchart/ac_regiao_2015.csv", delim = ";")
write_delim(ac_pais_2015, "../app/data/barchart/ac_pais_2015.csv", delim = ";")
# 2016
write_delim(ac_mun_2016, "../app/data/barchart/ac_mun_2016.csv", delim = ";")
write_delim(ac_micro_2016, "../app/data/barchart/ac_micro_2016.csv", delim = ";")
write_delim(ac_meso_2016, "../app/data/barchart/ac_meso_2016.csv", delim = ";")
write_delim(ac_uf_2016, "../app/data/barchart/ac_uf_2016.csv", delim = ";")
write_delim(ac_regiao_2016, "../app/data/barchart/ac_regiao_2016.csv", delim = ";")
write_delim(ac_pais_2016, "../app/data/barchart/ac_pais_2016.csv", delim = ";")
